# -*- coding: utf-8 -*-

"""
@Datetime: 2019/1/4
@Author: Zhang Yafei
"""
import queue
import uuid

from flask import Flask, render_template, request, jsonify, session

app = Flask(__name__)
app.secret_key = 'asdfasdfasd'

# app.config['DEBUG'] = True

USERS = {
    '1': {'name': '姚明', 'count': 1, 'image': 'static/images/姚明.jpg'},
    '2': {'name': '詹姆斯', 'count': 0, 'image': 'static/images/詹姆斯.jpg'},
    '3': {'name': '科比', 'count': 0, 'image': 'static/images/科比.jpg'},
}

QUEQUE_DICT = {
    # 'asdfasdfasdfasdf':Queue()
}


@app.route('/vote', methods=['GET', 'POST'])
def vote():
    uid = request.form.get('uid')
    USERS[uid]['count'] += 1
    for q in QUEQUE_DICT.values():
        q.put(USERS)
    return "投票成功"


@app.route('/get/vote', methods=['GET'])
def get_vote():
    user_uuid = session['current_user_uuid']
    q = QUEQUE_DICT[user_uuid]

    ret = {'status': True, 'data': None}
    try:
        users = q.get(timeout=5)
        ret['data'] = users
    except queue.Empty:
        ret['status'] = False

    return jsonify(ret)


@app.route('/home')
def home():
    user_uuid = str(uuid.uuid4())
    QUEQUE_DICT[user_uuid] = queue.Queue()

    session['current_user_uuid'] = user_uuid
    return render_template('home.html', users=USERS)


if __name__ == '__main__':
    # app.run(host='192.168.13.253',threaded=True)
    app.run(threaded=True)
